# Python 2.6.4
# Project Euler, Problem 207
# Copyright 2010 Talha Zaman

from mine import timed
import math

@timed
def P(num, den):
    n = 3
    perfect, total = 1, 1
    while perfect*den >= total*num:
        if math.log(n, 2) == int(math.log(n, 2)):
            perfect += 1
        total += 1
        n += 1
    n -= 1
    return n**2 - n

print P(1, 12345)
